package com.example.gulimall.order.interceptor;

import com.example.common.constant.AuthServerConstant;
import com.example.common.vo.MemberRespVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class LoginUserInterceptor implements HandlerInterceptor {
    public static  ThreadLocal<MemberRespVo> loginUser=new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws IOException {
        String uri = request.getRequestURI();
        //如果是这个结构的地址，直接放行
        boolean match = new AntPathMatcher().match("/order/status/**", uri);
        if (match){
            return true;
        }
        MemberRespVo attribute = (MemberRespVo)request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        if (attribute!=null){
            loginUser.set(attribute);
            return  true;
        }else {
            //没登陆，去登陆
                request.getSession().setAttribute("msg","请先登录");
                response.sendRedirect("http://auth.mall.com/login.html");
                return  false;


        }
    }
}
